package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

@CrossOrigin
@RestController
public class SwsSettingDetailController extends YxyApiBaseController {

    @Autowired
    SwsSettingRepository swsSettingRepository;

    @RequestMapping(value = { "/api_yw/manage/sch/platform/website/setting/detail", "/api_yw/manage/sch/platform/setting/detail" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != swsSetting, "未获取到学校配置信息");
        //
        String schoolLogo = swsSetting.getSchoolLogo();
        String schoolBadge = swsSetting.getSchoolBadge();
        if (StringUtils.isNotBlank(schoolLogo)) {
            schoolLogo = (schoolLogo.startsWith("http") || schoolLogo.startsWith("https")) ? schoolLogo : "https://yxcsp.91yunxiao.com/yxcsp/pull/" + schoolLogo;
            swsSetting.setSchoolLogo(schoolLogo);
        }
        if (StringUtils.isNotBlank(schoolBadge)) {
            schoolBadge = (schoolBadge.startsWith("http") || schoolBadge.startsWith("https")) ? schoolBadge : "https://yxcsp.91yunxiao.com/yxcsp/pull/" + schoolBadge;
            swsSetting.setSchoolBadge(schoolBadge);
        }
        RespModel respModel = new RespModel();
        respModel.swsSetting = swsSetting;
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiReqModel {
        public SwsSetting swsSetting;
    }
}
